import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store/store'

import Main from '@/views/Main'
import Auth from '@/views/auth/Auth'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Main',
      component: Main,
      meta: {
        title: '充值',
        background: '#272D36',
        requireAuth: false
      }
    },
    {
      path: '/auth',
      name: 'Auth',
      component: Auth,
      meta: {
        title: '授权'
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  //1、设置页面title、页面背景颜色
  if (to.meta.title !== undefined) window.document.title = to.meta.title
  document.body.style.backgroundColor =
    to.meta.background !== undefined ? to.meta.background : undefined

  //获取用户openId信息
  if (to.meta.requireAuth) {
    let openId = store.getters.user.openId
    if (openId == null && to.path != '/auth') {
      next('/auth')
      return false
    } else if (openId != null && to.path == '/auth') {
      next('/')
      return false
    }
  }
  next()
})

export default router
